package crud;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.junit.Test;

import Domain.News;
import utils.HibernateUtils;

public class TestCallStatement {
	
	@Test
	public void testBlob() throws Exception{
		
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		News news = new News();
		news.setAuthor("cc");
		news.setContent("CONTENT");
		news.setDate(new Date());
		news.setDesc("DESC");
		news.setTitle("CC");
		
		InputStream stream = new FileInputStream("Hydrangeas.jpg");
		Blob image = Hibernate.getLobCreator(session)
				              .createBlob(stream, stream.available());
		news.setImage(image);
		
		session.save(news);
		
		News news2 = (News) session.get(News.class, 1);
		Blob image1 = news2.getImage();
		
		InputStream in = image1.getBinaryStream();
		System.out.println(in.available());
		
		session.getTransaction().commit();
	}
	
	
	@Test
	public void testDynamicUpdate(){
		Session session = HibernateUtils.getSession();
		News news = (News) session.get(News.class, 1);
		session.beginTransaction();
		news.setAuthor("AABCD");
		session.getTransaction().commit();
	}
	
	@Test
	public void testDoword() {
		Session session = HibernateUtils.getSession();
		
		session.doWork(new Work() {
			
			@Override
			public void execute(Connection arg0) throws SQLException {
				//调用储存过程
			}
		});
		
	}
	

}
